import SocketProtocol from "./SocketProtocol";
import SocketRequest from "./SocketRequest";
import SocketConfig from "./SocketConfig";

export default class SocketManager {

    /**
     * 初始化全局唯一的游戏服务管理器。
     * @param _open 连接成功回调
     * @param _error 连接错误回调
     * @param _close 关闭连接回调
     * @param _response 接收到消息回调
     */
    public static init(_open: Function, _error: Function, _close: Function, _response: Function) {
        SocketRequest.connect(_open, _error, _close, _response);
    }

    /**
     * 是否与服务器处于连接状态
     */
    public static isConnect(): boolean {
        return SocketRequest.isConnect()
    }

    /**
     * 注册事件 没有注册的事件，会统一将消息发布到“统一的消息管理器”中（详见demoTS脚本）
     * @param protocol 事件的识别id
     * @param callback 事件回调
     */
    public static register(protocol: number, callback: Function) {
        SocketRequest.register(protocol, callback);
    }

    /**
     * 进入游戏 登录游戏
     * @param uid 玩家id 通过服务器获得
     * @param data 玩家数据 昵称、头像、段位、等级等数据
     */
    public static login(uid: number, data: string) {
        let request = { "protocol": SocketProtocol.LOGINGAME, "uid": uid, "data": data, "app_id": SocketConfig.appid };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 退出游戏 断开连接
     * @param uid 玩家id 通过服务器获得
     */
    public static logout(uid: number) {
        let request = { "protocol": SocketProtocol.LOGOUTGAME };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 发送心跳(每20-30秒发送一次心跳，避免长时间没有发送消息被踢下线)
     */
    public static heartBeat() {
        let request = { "protocol": SocketProtocol.HEARTBEAT };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 匹配房间（无空闲房间时，自动创建房间）
     * @param room_tag 房间标签 用于判断房间的游戏模式
     * @param player_count 房间最大玩家数量
     */
    public static matchRoom(room_tag: string, player_count: number) {
        let request = { "protocol": SocketProtocol.MATCHROOM, "room_tag": room_tag, "player_count": player_count };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 加入房间
     * @param room_tag 房间标签 用于判断房间的游戏模式
     * @param room_id 房间id
     */
    public static joinRoom(room_tag: string, room_id: string) {
        let request = { "protocol": SocketProtocol.JOINROOM, "room_tag": room_tag, "room_id": room_id };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 离开房间
     */
    public static leaveRoom() {
        let request = { "protocol": SocketProtocol.LEAVEROOM, "appid": SocketConfig.appid };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 准备
     */
    public static readyGame() {
        let request = { "protocol": SocketProtocol.READYGAME, "appid": SocketConfig.appid };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 加载完成
     */
    public static loadedGame() {
        let request = { "protocol": SocketProtocol.LOADEDGAME, "appid": SocketConfig.appid };
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 发送广播
	 * @param data 发送的数据
     * @param key 广播识别码
     * @param isReturn 是否广播给发送者
     */
    public static broadcast(data: string, key:string, isReturn:boolean) {
        let request = { "protocol": SocketProtocol.SEBROADCAST, "data": data, "key":key, "isReturn":isReturn?1:0};
        SocketRequest.send(JSON.stringify(request));
    }

    /**
     * 获取房间玩家数据
     */
    public static getRoomPlayers() {
        let request = { "protocol": SocketProtocol.GETPLAYERS };
        SocketRequest.send(JSON.stringify(request));
    }
}
